package com.ruoyi.asset.workman.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.asset.roadgreening.domain.vo.RoadGreeningVo;
import com.ruoyi.asset.workman.domain.bo.WorkmanSearchBo;
import com.ruoyi.asset.workman.domain.vo.WorkmanVo;
import com.ruoyi.asset.workman.service.IWorkmanService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 作业人员
 *
 * @author cc
 * @date 2023-08-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/asset/workman")
public class WorkmanController extends BaseController {

	private final IWorkmanService workmanService;

	/**
	 * 导入作业人员数据
	 */
    //@SaCheckPermission("standingbook:cleaningImportBase:add")
	@SaIgnore()
    @Log(title = "作业人员导入", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping(value = "/importData")
	public R<List<String>> importData(@RequestParam String excelUrl, @RequestParam String serviceType) {
        List<String> list = this.workmanService.importExcel(excelUrl, serviceType);

        if (list == null) {
            return R.fail();
        } else {
            return R.ok(list);
        }
	}

	/**
	 * 导出作业人员模板
	 */
	@SaIgnore()
	@PostMapping("/importTemplate")
	public void exportExcel(HttpServletResponse response) {
		ExcelUtil.exportExcel(new ArrayList<>(), "作业人员导入模板", WorkmanVo.class, response);
	}

    /**
     * 删除作业人员数据
     */
    @SaIgnore()
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                              @PathVariable Long[] ids) {
        return toAjax(workmanService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 查询作业人员信息
     */
    @SaIgnore()
    @GetMapping("/list")
    public TableDataInfo<WorkmanVo> list(WorkmanSearchBo searchBo, PageQuery pageQuery) {
        return this.workmanService.queryPageList(searchBo, pageQuery);
    }

    /**
     * 导出作业人员
     */
    @SaIgnore()
    //@Log(title = "作业人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    //public void export(@RequestBody(required = false) WorkmanSearchBo searchBo, HttpServletResponse response)
    public void export(WorkmanSearchBo searchBo, HttpServletResponse response) {
        List<WorkmanVo> list = this.workmanService.exportWorkman(searchBo);

        ExcelUtil.exportExcel(list, "作业人员", WorkmanVo.class, response);
    }
}
